/**
 * <p>查询条件按钮控件</p>
 * xtype = cceafconditionbutton

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.ConditionButton
 * @extends Ext.button.Split
 * @since 1.0
 */
Ext.define('cceaf.control.ConditionButton',{
	extend: 'Ext.button.Split', 
    alias: 'widget.cceafconditionbutton',

	constructor: function(config){
		config = config || {};
        
        var menu = new Ext.menu.Menu();
        
        var conditions = config.conditions;
        if(conditions){
	        for(var i=0;i<conditions.length;i++){
	        	var checked = false;
	        	if(conditions[i][0].checked){//每组查询条件的第1个需设置是否为默认查询条件
	        		checked = conditions[i][0].checked;
	        	}
	        	
	        	var menucheckitem = {
		        	xtype: 'menucheckitem',
		        	text: conditions[i][0].text,
		        	conditionid: conditions[i][0].id, //每组查询条件的第1需设置
		        	checked: checked
		        };
        		menu.add(menucheckitem);
			}
		}
        
        
		config = Ext.applyIf(config || {}, {
			menu: menu
		});

		cceaf.control.ConditionButton.superclass.constructor.apply(this, arguments);
	},
	
	iconCls: 'page_conditionIcon',
	
	/**
	 * 查询条件，例子<br>
	   var conditions = [<br>
    	&nbsp;&nbsp;[<br>
    		&nbsp;&nbsp;&nbsp;&nbsp;{xtype: 'label', id: 'q_title_label',text: '标题', width: 80, checked: true},<br>
			&nbsp;&nbsp;&nbsp;&nbsp;{xtype: 'cceaftextfield',id: 'q_title'},<br>
			&nbsp;&nbsp;&nbsp;&nbsp;{xtype: 'tbtext', width: 80}<br>
    	&nbsp;&nbsp;],<br>
		&nbsp;&nbsp;[<br>
    		&nbsp;&nbsp;&nbsp;&nbsp;{xtype: 'label', id: 'q_themeWord_label',text: '主题词', width: 80, checked: true},<br>
			&nbsp;&nbsp;&nbsp;&nbsp;{xtype: 'cceaftextfield',id: 'q_themeWord'},<br>
			&nbsp;&nbsp;&nbsp;&nbsp;{xtype: 'tbtext', width: 80}<br>
    	&nbsp;&nbsp;]<br>
       ];<br>
     
     * @property conditions 
     * @type array
     * @default null
     */
	conditions: null,
    
	initEvents: function() {
    	cceaf.control.ConditionButton.superclass.initEvents.call(this);
    	
    	//注册选择查询条件的事件
    	var menucheckitems = this.menu.query('menucheckitem');
    	if(menucheckitems.length){
	    	for(var i=0;i<menucheckitems.length;i++){
	    		menucheckitems[i].on('click',this.menucheckitemHandler);
	    	}
    	}
    },
    
    menucheckitemHandler: function(){
    	var queryGrid = this.up('gridpanel');
    	var conditionToolbars = queryGrid.query('cceafconditiontoolbar');
    	
    	//得到已经存在的查询条件数量
    	var existConditionCount = 0;
    	for(i=0;i<conditionToolbars.length;i++){
    		existConditionCount += conditionToolbars[i].query('label').length;
    	}
    	//alert(existConditionCount);
    	
    	//cceafconditiontoolbar数量
    	conditionToolbarCount = conditionToolbars.length;
		//alert(conditionToolbarCount);
    	
    	//var conditions = Ext.getCmp('btnCondition').conditions;	
		var conditions = this.up('cceafconditionbutton').conditions;
		
		if(this.checked){//增加查询条件
			for(i=0;i<conditions.length;i++){
				if(conditions[i][0].id == this.conditionid){
					//如果已经存在的existConditionCount是3的倍数，需要增加1个cceafconditiontoolbar
					if(existConditionCount % 3 == 0){
						var addToolbar = new cceaf.control.ConditionToolbar();
						addToolbar.add(conditions[i]);
						queryGrid.insertDocked(conditionToolbarCount + 2,addToolbar);
						
						conditionToolbarCount ++;
					}
					else{
						//最后增加的索引为0
						conditionToolbars[conditionToolbarCount-1].add(conditions[i]);
					}
					break;
				}
			}
		}
		else{//删除查询条件
			//查找要删除的condition
			var deleteCondition;
			for(i=0;i<conditions.length;i++){
				if(conditions[i][0].id == this.conditionid){
					deleteCondition = conditions[i];
					break;
				}
			}
			
			for(i=0;i<conditionToolbars.length;i++){
				var conditionLabels = conditionToolbars[i].query('label');
				for(j=0;j<conditionLabels.length;j++){
					var conditionid = conditionLabels[j].id;
					if(conditionid == this.conditionid){
						//移除查询条件
						for(k=0;k<deleteCondition.length;k++){
							conditionToolbars[i].remove(deleteCondition[k].id);
						}
						break;
					}
				}
			}
			
			//查找所有存在的conditions
			var existConditions = new Array();
			existConditionCount = 0;
			//最后增加的索引为0
			for(i=conditionToolbars.length-1;i>=0;i--){
				var conditionLabels = conditionToolbars[i].query('label');
				for(j=0;j<conditionLabels.length;j++){
					for(k=0;k<conditions.length;k++){
						if(conditionLabels[j].id == conditions[k][0].id){
							existConditions[existConditionCount] = conditions[k];
							existConditionCount ++;
						}
					}
				}
			}
			
			//移除所有的conditions
			for(i=0;i<conditionToolbars.length;i++){
				queryGrid.removeDocked(conditionToolbars[i]);
			}
			
			//排序后加载所有的conditions
			var count = 0;
			for(i=0;i<existConditions.length;i++){
				if(i % 3 == 0){
					addToolbar = new cceaf.control.ConditionToolbar();
					addToolbar.add(existConditions[i]);
					queryGrid.insertDocked(count+1,addToolbar);
					count ++;
				}
				else{
					addToolbar.add(existConditions[i]);
				}
			}
		}
    }
    
});